package com.my.quartz.hello;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;

/**
 * @Author ScholarTang
 * @Date 2021/7/13 10:53
 * @Desc 调度器
 */
public class HelloSchedulerDemo {
    public static void main(String[] args) throws SchedulerException {
        //从调度工厂中获取调度器实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //通过JobBuilder构建一个任务实例
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                //设置任务的唯一实例名称和任务组名称组名
                .withIdentity("job1", "group1")
                //构建实例
                .build();
        //通过TriggerBuilder构建触发器实例
        Trigger trigger = TriggerBuilder.newTrigger()
                //设置触发器唯一实例名称和触发器的组名
                .withIdentity("trigger1", "group1")
                //执行计划，每五秒执行一次
                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ? *"))
                //构建实例
                .build();
        //调度器绑定任务实例和触发器
        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.getListenerManager().addJobListener(new MyJobListener(), KeyMatcher
                //jobKey中的name参数和group参数对应的就是任务实例【JobDetail】的name和group
                .keyEquals(JobKey.jobKey("job1","group1")));
        //开启定时任务
        scheduler.start();
    }
}
